﻿using System;
using System.AutoUpdate.Library;
using System.AutoUpdate.Security;
using System.AutoUpdate.Sources;
using System.AutoUpdate.Tools;
using System.Collections.Generic;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace System.AutoUpdate.Test
{
    [TestClass]
    public class TextWebSourceTest
    {
        [TestMethod]
        public void TextWebSourceVersionTest1 ()
        {
            //
            // None test
            //

            TextWebSource test = new TextWebSource ("http://www.arvystate.net/test/AutoUpdate/", "patches_none.txt", "Download");
            
            List<UpdateVersionInfo> versions = test.GetVersions ();
            
            Assert.IsTrue (versions.Count == 0, "No patches in current file.");

            //
            // Wrong patch location
            //

            test = new TextWebSource("http://www.arvystate.net/test/AutoUpdate/", "patches_notexisting.txt", "Download");

            versions = test.GetVersions();

            Assert.IsTrue(versions.Count == 0, "Version file does not exist.");

            //
            // Wrong URI
            //

            test = new TextWebSource("http://www.randomnotexistinguri-must-be-lame.ca/", "patches_notexisting.txt", "Download");

            versions = test.GetVersions();

            Assert.IsTrue(versions.Count == 0, "Unavailable URI.");

            //
            // Count test
            //

            test = new TextWebSource("http://www.arvystate.net/test/AutoUpdate/", "patches_two.txt", "Download");

            versions = test.GetVersions();

            Assert.IsTrue(versions.Count == 2, "Count is correct.");

            test = new TextWebSource("http://www.arvystate.net/test/AutoUpdate/", "patches.txt", "Download");

            versions = test.GetVersions();

            Assert.IsTrue(versions.Count == 3, "Count is correct.");
        }

        [TestMethod]
        public void TextWebSourceVersionTest2 ()
        {
            //
            // Version parsing test
            //

            TextWebSource test = new TextWebSource("http://www.arvystate.net/test/AutoUpdate/", "patches.txt", "Download");

            List<UpdateVersionInfo> versions = test.GetVersions();

            Assert.IsTrue(versions[0].UpdateVersion == new Version("1.1.0.0"), "Version parsing is incorrect.");
            Assert.IsTrue(versions[1].UpdateVersion == new Version("1.0.1.0"), "Version parsing is incorrect.");
            Assert.IsTrue(versions[2].UpdateVersion == new Version("1.0.0.1"), "Version parsing is incorrect.");

            //
            // DateTime parsing test
            //

            Assert.IsTrue(versions[0].UpdateReleaseDate == DateTime.Parse("25-1-2011 23:12:32"), "DateTime parsing is incorrect.");
            Assert.IsTrue(versions[1].UpdateReleaseDate == DateTime.Parse("11-12-2010 13:42:22"), "DateTime parsing is incorrect.");
            Assert.IsTrue(versions[2].UpdateReleaseDate == DateTime.Parse("16-05-2009"), "DateTime parsing is incorrect.");

            //
            // Remote Path parsing test
            //

            Assert.IsTrue(versions[0].File.RemotePath == "http://www.arvystate.net/test/AutoUpdate/Download/", "Remote Path parsing is incorrect.");
            Assert.IsTrue(versions[1].File.RemotePath == "http://www.arvystate.net/test/AutoUpdate/Download/", "Remote Path parsing is incorrect.");
            Assert.IsTrue(versions[2].File.RemotePath == "http://www.arvystate.net/test/AutoUpdate/Download/", "Remote Path parsing  is incorrect.");

            //
            // Hash test
            //

            Assert.IsTrue(versions[0].File.Hash == "1B3FB916D942171E87292AE94219F8674D69706D", "Hash parsing is incorrect.");
            Assert.IsTrue(versions[1].File.Hash == "754C1A26A83BEF42921D496BA29928DF2E345C44", "Hash parsing is incorrect.");
            Assert.IsTrue(versions[2].File.Hash == "65BB648047621BF84B27E72D6572C3A910C4CBD6", "Hash parsing is incorrect.");

            //
            // Hash type test
            //

            Assert.IsTrue(versions[0].File.HashType == HashAlgorithmType.Sha1, "Hash type parsing is incorrect.");
            Assert.IsTrue(versions[1].File.HashType == HashAlgorithmType.Sha1, "Hash type parsing is incorrect.");
            Assert.IsTrue(versions[2].File.HashType == HashAlgorithmType.Sha1, "Hash type parsing is incorrect.");

            //
            // File size test
            //

            Assert.IsTrue(versions[0].File.FileSize == 59143, "File size method broken.");
            Assert.IsTrue(versions[1].File.FileSize == 76557, "File size method broken.");
            Assert.IsTrue(versions[2].File.FileSize == 67029, "File size method broken.");
        }

        [TestMethod]
        public void TextWebSourceHasVersionTest ()
        {
            TextWebSource test = new TextWebSource("http://www.arvystate.net/test/AutoUpdate/", "patches.txt", "Download");
            
            UpdateVersionInfo info1 = new UpdateVersionInfo (new Version("1.0.0.0"), 0, DateTime.Now);
            UpdateVersionInfo info2 = new UpdateVersionInfo (new Version("1.0.1.0"), 0, DateTime.Now);
            UpdateVersionInfo info3 = new UpdateVersionInfo (new Version("1.1.0.0"), 0, DateTime.Now);
            UpdateVersionInfo info4 = new UpdateVersionInfo (new Version("1.0.0.1"), 0, DateTime.Now);

            Assert.IsTrue(!test.HasVersion(info1), "Has version test failed.");
            Assert.IsTrue(test.HasVersion(info2), "Has version test failed.");

            Assert.IsTrue(test.HasVersion(info3), "Has version test failed.");
            Assert.IsTrue(test.HasVersion(info4), "Has version test failed.");
        }

        [TestMethod]
        public void TextWebSourceTransferTest ()
        {
            TextWebSource test = new TextWebSource("http://www.arvystate.net/test/AutoUpdate/", "patches.txt", "Download");

            test.TransferEnded += (sender, args) =>
                                  {
                                      
                                  };

            List<UpdateVersionInfo> versions = test.GetVersions();

            test.Transfer (versions[0]);
        }
    }
}
